﻿using System;
using System.Collections.Generic;
using System.Linq;
using UMC.Data;
using UMC.WorkFlow.Entities;

namespace UMC.WorkFlow
{
    public class DataFactory
    {
        static DataFactory()
        {
            HotCache.Register<TaskCheckContorl>("ItemId", "ShapeId");
            HotCache.Register<TaskFormControl>("ItemId").Register("TaskId", "ForId", "ItemId");
            HotCache.Register<TaskCommand>("TaskId", "ShapeId", "CommandType", "TaskStatus").Register("TaskId", "TaskStatus");
            HotCache.Register<TaskKeyValue>("KeyId", "Value");//.Register("TaskId", "KeyId", "Value");
            HotCache.Register<TaskKey>("Id").Register("Code", "Id");
            HotCache.Register<TaskStaff>("TaskId", "DesignKey");
            HotCache.Register<TaskDesign>("Id").Register("TaskId", "Id");
            HotCache.Register<TaskData>("InstanceId", "DataName", "DataIndex");
            HotCache.Register<TaskObSerialize>("InstanceId").Register("TaskId", "InstanceId");
            HotCache.Register<TaskForm>("TaskId").Register("Code", "TaskId");


            HotCache.Register<TaskApproving>("InstanceId", "ShapeId").Register("TaskId", "InstanceId", "ShapeId").Register("Username", "TaskId", "InstanceId", "ShapeId");
            HotCache.Register<TaskHistory>("IdentityName", "InstanceId").Register("InstanceId", "IdentityName");//.Register("Username", "TaskId", "InstanceId", "ShapeId");
            HotCache.Register<TaskLog>("Id").Register("InstanceId", "ShapeId", "Id");//.Register("Username", "TaskId", "InstanceId", "ShapeId");
            HotCache.Register<TaskRequest>("InstanceId").Register("TaskId", "InstanceId").Register("Username", "TaskId", "InstanceId");

            HotCache.Register<TaskMessage>("Id").Register("TaskId", "InstanceId", "Id").Register("Username", "TaskId", "InstanceId", "Id");
            HotCache.Register<TaskSubscribe>("TaskId", "ShapeId", "Username");


            HotCache.Register<TaskSync>("Id");


        }
        static DataFactory _Instance;// = new DataFactory();

        public static DataFactory Instance()
        {
            if (_Instance == null)
            {
                _Instance = new DataFactory();
            }
            return _Instance;
        }
        public void Put(TaskLog taskLog)
        {
            HotCache.Put(taskLog);
        }
        public void Put(TaskObSerialize taskLog)
        {
            HotCache.Put(taskLog);
        }
        public void Put(TaskSubscribe taskLog)
        {
            HotCache.Put(taskLog);
        }
        public void Delete(TaskSubscribe taskLog)
        {
            HotCache.Delete(taskLog);
        }
        public TaskSubscribe[] TaskSubscribe(ulong instanceId)
        {
            return HotCache.Find(new TaskSubscribe() { TaskId = instanceId }, true, 0, 10000, out var _);

            // return HotCache.Get(new TaskKey() { Id = KeyId });//, true, 0, 10000, out var _);
        }
        public TaskLog[] TaskLog(ulong instanceId, String ShapeId)
        {
            return HotCache.Find(new TaskLog() { InstanceId = instanceId, ShapeId = ShapeId }, true, 0, 10000, out var _);

            // return HotCache.Get(new TaskKey() { Id = KeyId });//, true, 0, 10000, out var _);
        }
        public TaskFormControl TaskFormControl(ulong KeyId)
        {
            return HotCache.Get(new TaskFormControl() { ItemId = KeyId });//, true, 0, 10000, out var _);
        }

        public TaskKey TaskKey(ulong KeyId)
        {
            return HotCache.Get(new TaskKey() { Id = KeyId });//, true, 0, 10000, out var _);
        }
        public TaskKey TaskKey(String code)
        {
            return HotCache.Get(new TaskKey() { Code = code });//, true, 0, 10000, out var _);
        }
        public TaskObSerialize TaskObSerialize(ulong code)
        {
            return HotCache.Get(new TaskObSerialize() { InstanceId = code });//, true, 0, 10000, out var _);
        }

        public TaskFormControl[] TaskFormControl(ulong taskId, ulong forid)
        {
            return HotCache.Find(new TaskFormControl() { TaskId = taskId, ForId = forid }, true, 0, 10000, out var _);
        }
        public TaskApproving[] TaskApproving(ulong InstanceId)
        {
            return HotCache.Find(new TaskApproving() { InstanceId = InstanceId }, true, 0, 10000, out var _);
        }
        public TaskKeyValue[] TaskKeyValue(ulong KeyId)
        {
            return HotCache.Find(new TaskKeyValue() { KeyId = KeyId }, true, 0, 10000, out var _);
        }
        public TaskKeyValue TaskKeyValue(ulong KeyId, String value)
        {
            return HotCache.Get(new TaskKeyValue() { KeyId = KeyId, Value = value });//, true, 0, 10000, out var _);
        }
        public TaskCommand[] TaskCommand(ulong instanceId)
        {
            return HotCache.Find(new TaskCommand() { TaskId = instanceId }, true, 0, 10000, out var _);
        }
        public TaskCommand[] TaskCommand(ulong instanceId, TaskStatus taskStatus)
        {
            return HotCache.Find(new TaskCommand() { TaskId = instanceId, TaskStatus = taskStatus }, true, 0, 10000, out var _);
        }

        public TaskStaff[] TaskStaff(ulong taskId)
        {

            return HotCache.Find(new TaskStaff() { TaskId = taskId }, true, 0, 10000, out var _);

        }

        public TaskForm TaskForm(String code)
        {

            return HotCache.Find(new TaskForm() { Code = code }, false, 0, 1, out var _).FirstOrDefault();

        }
        public TaskForm TaskForm(ulong taskId)
        {

            return HotCache.Get(new TaskForm() { TaskId = taskId });//, true, 0, 10000, out var _);

        }
        public TaskRequest TaskRequest(ulong taskId)
        {

            return HotCache.Get(new TaskRequest() { InstanceId = taskId });//, true, 0, 10000, out var _);

        }
        //  DataFactory.Instance().TaskForm(formId);
        public TaskStaff TaskStaff(ulong taskId, String ShapeId)
        {

            return HotCache.Get(new TaskStaff() { TaskId = taskId, DesignKey = ShapeId });//, true, 0, 10000, out var _);

        }
        public TaskCommand[] TaskCommand(ulong instanceId, String ShapeId)
        {
            return HotCache.Find(new TaskCommand() { TaskId = instanceId, ShapeId = ShapeId }, true, 0, 10000, out var _);
        }
        public TaskDesign TaskDesign(ulong instanceId)
        {
            return HotCache.Get(new TaskDesign() { Id = instanceId });//, true, 0, 10000, out var _);
        }
        public TaskDesign[] TaskDesigns(ulong task)
        {
            return HotCache.Find(new TaskDesign() { TaskId = task }, true, 0, 10000, out var _);
        }
        public TaskData[] TaskData(ulong instanceId)
        {
            return HotCache.Find(new TaskData() { InstanceId = instanceId }, true, 0, 10000, out var _);
        }
        public TaskData[] TaskData(ulong instanceId, String name)
        {
            return HotCache.Find(new TaskData() { InstanceId = instanceId, DataName = name }, true, 0, 10000, out var _);
        }
        public void Put(TaskCommand taskData)
        {
            HotCache.Put(taskData);//.Find(new TaskData() { InstanceId = instanceId }, true, 0, 10000, out var _);
        }
        public void Put(TaskKey taskData)
        {
            HotCache.Put(taskData);//.Find(new TaskData() { InstanceId = instanceId }, true, 0, 10000, out var _);
        }
        public void Put(TaskStaff taskData)
        {
            HotCache.Put(taskData);//.Find(new TaskData() { InstanceId = instanceId }, true, 0, 10000, out var _);
        }
        public void Put(TaskForm taskData)
        {
            HotCache.Put(taskData);//.Find(new TaskData() { InstanceId = instanceId }, true, 0, 10000, out var _);
        }
        public void Delete(TaskStaff taskData)
        {
            HotCache.Delete(taskData);//.Find(new TaskData() { InstanceId = instanceId }, true, 0, 10000, out var _);
        }
        public void Delete(TaskKeyValue taskData)
        {
            HotCache.Delete(taskData);//.Find(new TaskData() { InstanceId = instanceId }, true, 0, 10000, out var _);
        }
        public void Put(TaskKeyValue taskData)
        {
            HotCache.Put(taskData);//.Find(new TaskData() { InstanceId = instanceId }, true, 0, 10000, out var _);
        }
        public void Put(TaskDesign taskData)
        {
            HotCache.Put(taskData);//.Find(new TaskData() { InstanceId = instanceId }, true, 0, 10000, out var _);
        }
        public void Put(TaskFormControl taskData)
        {
            HotCache.Put(taskData);//.Find(new TaskData() { InstanceId = instanceId }, true, 0, 10000, out var _);
        }
        public void Delete(TaskFormControl taskData)
        {
            HotCache.Delete(taskData);//.Find(new TaskData() { InstanceId = instanceId }, true, 0, 10000, out var _);
        }
        public void Put(TaskData taskData)
        {
            HotCache.Put(taskData);//.Find(new TaskData() { InstanceId = instanceId }, true, 0, 10000, out var _);
        }
        public void Delete(TaskData taskData)
        {
            HotCache.Delete(taskData);//.Find(new TaskData() { InstanceId = instanceId }, true, 0, 10000, out var _);
        }
        public void Put(TaskCheckContorl taskData)
        {
            HotCache.Put(taskData);//.Find(new TaskData() { InstanceId = instanceId }, true, 0, 10000, out var _);
        }
        public void Delete(TaskCheckContorl taskData)
        {
            HotCache.Delete(taskData);//.Find(new TaskData() { InstanceId = instanceId }, true, 0, 10000, out var _);
        }
        public void Put(TaskApproving taskData)
        {
            HotCache.Put(taskData);//.Find(new TaskData() { InstanceId = instanceId }, true, 0, 10000, out var _);
        }
        public void Put(TaskHistory taskData)
        {
            HotCache.Put(taskData);//.Find(new TaskData() { InstanceId = instanceId }, true, 0, 10000, out var _);
        }
        public TaskHistory[] TaskHistory(ulong instanceId)
        {
            return HotCache.Find(new TaskHistory { InstanceId = instanceId }, false, 0, 10000, out var _);
        }
        public void Delete(TaskApproving taskData)
        {
            HotCache.Delete(taskData);//.Find(new TaskData() { InstanceId = instanceId }, true, 0, 10000, out var _);
        }
        public void Delete(TaskRequest taskData)
        {
            HotCache.Delete(taskData);//.Find(new TaskData() { InstanceId = instanceId }, true, 0, 10000, out var _);
        }
        public void Put(TaskRequest taskData)
        {
            HotCache.Put(taskData);//.Find(new TaskData() { InstanceId = instanceId }, true, 0, 10000, out var _);
        }
        public void Put(TaskMessage taskData)
        {
            HotCache.Put(taskData);//.Find(new TaskData() { InstanceId = instanceId }, true, 0, 10000, out var _);
        }
        public void Put(IEnumerable<TaskMessage> taskData)
        {
            foreach (var v in taskData)
            {
                HotCache.Put(v);
            }
            //.Find(new TaskData() { InstanceId = instanceId }, true, 0, 10000, out var _);
        }
    }
}
